package com.iplant.mes.controller.qms;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.QMSService;
import com.iplant.mes.service.po.qms.QMSMaterialCheck;
import com.iplant.wms.service.po.batch.MSSBatchRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 原料检验单
 *
 * @author femi
 * 2024-2-2
 */
@Slf4j
@RestController
@RequestMapping("/api/QMSMaterialCheck")
@Api(tags = "来料检验接口")
public class QMSMaterialCheckController extends BasicController<QMSMaterialCheck> {


    public QMSMaterialCheckController() {
        super(QMSMaterialCheck.class);
        mBatchRecordService = ORMUtils.GetPoService(MSSBatchRecord.class);
    }

    private PoService<MSSBatchRecord> mBatchRecordService;

    @Autowired
    QMSService mQMSService;


    @TraceAPI("创建批次记录")
    @PostMapping("/CreateBatch")
    @ApiOperation(value = "创建批次记录")
    public Object CreateBatch(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }
            // 获取参数
            QMSMaterialCheck wQMSMaterialCheck = CloneTool.Clone(wParam.get("data"), mClazz);
            if (wQMSMaterialCheck == null) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<MSSBatchRecord> wServiceResult = mQMSService.QMS_CreateBatchRecordByMaterialCheck(wLoginUser, wQMSMaterialCheck);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wQMSMaterialCheck);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode, wServiceResult.Result, wQMSMaterialCheck);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }


    /**
     * 批量删除
     */
    @TraceAPI("批量删除")
    @PostMapping("/DeleteList")
    public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);


            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<QMSMaterialCheck> wMSSBatchRuleList = CloneTool.CloneArray(wParam.get("data"), mClazz);
            ServiceResult<Integer> wServiceResult = new ServiceResult<>(0);
            for (QMSMaterialCheck wMSSBatchRule : wMSSBatchRuleList) {

                wServiceResult = mQMSService.QMS_DeleteMaterialCheck(wLoginUser, wMSSBatchRule);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @TraceAPI("删除")
    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            QMSMaterialCheck wMSSBatchRule = CloneTool.Clone(wParam.get("data"), mClazz);

            ServiceResult<Integer> wServiceResult = mQMSService.QMS_DeleteMaterialCheck(wLoginUser, wMSSBatchRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

}
